package com.example.shopping_li.goods;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.shopping_li.R;
import com.example.shopping_li.URLUtils;
import com.squareup.picasso.Picasso;

import java.util.List;

public class GoodsAdapter extends BaseAdapter {
    Context context;

    private List<GoodsTypeBean.DataBean> data;
    public GoodsAdapter(Context context, List<GoodsTypeBean.DataBean> data) {
        this.context = context;
        this.data = data;
    }

    @Override
    public int getCount() {
        return data.size();
    }

    @Override
    public Object getItem(int position) {
        return data.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        VH vh;
        if (convertView==null){
            convertView= LayoutInflater.from(context).inflate(R.layout.item_goods_type,null);
            vh=new VH(convertView);
            convertView.setTag(vh);
        }else{
             vh = (VH) convertView.getTag();

        }
        GoodsTypeBean.DataBean bean = data.get(position);
        vh.nameTv.setText(bean.getName());
        vh.desTv.setText(bean.getInfo());
        vh.priceTv.setText("￥"+bean.getPrice()+"元");
        String picUrl= URLUtils.PUBLIC_URL+bean.getPhoto();
        Picasso.with(context).load(picUrl).into(vh.iv);
        return convertView;
    }
    class VH{
        ImageView iv;
        TextView nameTv,desTv,priceTv;
        public VH(View view){
            iv=view.findViewById(R.id.frag_hot_tv02);
            nameTv=view.findViewById(R.id.frag_hot_iv1);
            desTv=view.findViewById(R.id.item_goods_type_tv2);
            priceTv=view.findViewById(R.id.frag_hot_tv03);
        }
    }
}
